package com.huawei.hwCloudJs;

import android.text.TextUtils;
import android.util.Log;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
import com.huawei.hwCloudJs.api.JsParam;
import com.huawei.hwCloudJs.core.JsCallback;
import com.huawei.hwCloudJs.core.a.a;
import com.huawei.hwCloudJs.core.d;
import com.huawei.hwCloudJs.d.c;
import com.huawei.hwCloudJs.d.f;
import com.huawei.hwCloudJs.service.jsapi.JsCoreApi.ChannelMessageReq;
import com.huawei.hwCloudJs.service.jsmsg.NativeMsg;
import com.huawei.hwCloudJs.support.b.b;
import com.huawei.hwCloudJs.support.enables.INoProguard;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.json.JSONObject;

public class HWCloudJSBridge implements b<NativeMsg>, INoProguard {
    private static final String a = "HWCloudJSBridge";
    private WebView b;
    private String c;
    private final List<String> d;
    private HashMap<String, String> e;

    public HWCloudJSBridge() {
        this.b = null;
        this.c = "";
        this.d = new ArrayList();
        this.e = new HashMap();
        this.c = (((long) f.a()) + System.currentTimeMillis()) + "";
    }

    private void a(a aVar, String str, String str2, String str3, String str4) {
        Log.i(a, "JS callNative begin");
        Object obj = null;
        if (aVar.a() != null) {
            if (aVar.a() == JsParam.class) {
                obj = new JsParam(str3, str4, this.c);
            } else {
                obj = new com.huawei.hwCloudJs.core.b().a(str, aVar.a());
                if (obj != null && (obj instanceof JsParam)) {
                    obj = c.a((JsParam) obj, str3, str4, this.c);
                }
            }
        }
        aVar.a(obj, new JsCallback(this.b, str2, this.c));
    }

    private void a(NativeMsg nativeMsg, String str) {
        if (b(str)) {
            Log.e(a, "url white contains!!");
            c.a(this.b, nativeMsg);
        } else if (nativeMsg.isOpen()) {
            synchronized (this.d) {
                for (String a : this.d) {
                    if (a(a, str, nativeMsg.getPermission())) {
                        c.a(this.b, nativeMsg);
                    }
                }
            }
        }
    }

    private void a(String str, String str2, String str3, String str4, a aVar) {
        if (b(str4)) {
            Log.e(a, "JS url white contains!!");
            a(aVar, str2, str3, str, str4);
        } else if (!c.a(aVar)) {
            Log.e(a, "JS method Is not Open!!");
            c.a(this.b, str3, 12, this.c);
        } else if (a(str, str4, c.c(aVar))) {
            Log.i(a, "JS checkAuth OK");
            a(aVar, str2, str3, str, str4);
        } else {
            Log.e(a, "JS checkAuth fail!");
            c.a(this.b, str3, 10, this.c);
        }
    }

    private void a(String str, String str2, List<String> list, JSONObject jSONObject) {
        Log.i(a, "JS configAuth");
        1 1 = new 1(this, str, str2, list, jSONObject);
        List arrayList = new ArrayList();
        com.huawei.hwCloudJs.service.a.b.a().a(str, str2, arrayList, 1, this.b.getContext());
    }

    private boolean a(String str) {
        JSONObject a = c.a(str);
        if (a != null) {
            String optString = a.optString("eventName");
            if (this.e.containsKey(optString)) {
                return true;
            }
            if (optString.length() < 64 && this.e.size() < 50) {
                this.e.put(optString, "");
                return true;
            }
        }
        return false;
    }

    private boolean a(String str, String str2, String str3) {
        List arrayList = new ArrayList();
        if (!TextUtils.isEmpty(str3)) {
            arrayList.add(str3);
        }
        return com.huawei.hwCloudJs.service.a.b.a().a(str, str2, arrayList);
    }

    private void b(String str, String str2, List<String> list, JSONObject jSONObject) {
        d.a().a(this.b.getContext(), str, str2, this.c, list, jSONObject);
    }

    private boolean b(String str) {
        return com.huawei.hwCloudJs.service.a.b.a().b(str);
    }

    public void attach(WebView webView) {
        this.b = webView;
        webView.addJavascriptInterface(this, "hbssdk");
        com.huawei.hwCloudJs.service.jsmsg.a.a().a((b) this);
        Log.i(a, "JS attach");
    }

    @JavascriptInterface
    public void config(String str) {
        Log.i(a, "JS config");
        JSONObject a = c.a(str);
        if (a == null) {
            Log.e(a, "JS callJson == null");
            c.a(this.b, 13);
            return;
        }
        Object optString = a.optString("appId");
        if (TextUtils.isEmpty(optString)) {
            Log.e(a, "JS appId isEmpty");
            c.a(this.b, 13);
            return;
        }
        Object a2 = com.huawei.hwCloudJs.d.a.a(this.b);
        if (TextUtils.isEmpty(a2)) {
            Log.e(a, "JS webViewUrl isEmpty");
            c.a(this.b, b.k);
            return;
        }
        a(optString, a2, com.huawei.hwCloudJs.d.b.a(a.optJSONArray("needConfig")), a);
        synchronized (this.d) {
            this.d.add(optString);
        }
        this.b.getContext().getApplicationContext();
    }

    public void detach() {
        this.b.removeJavascriptInterface("hbssdk");
        com.huawei.hwCloudJs.service.jsmsg.a.a().b(this);
        Log.i(a, "JS detach");
    }

    public String getBridgeId() {
        return this.c;
    }

    @JavascriptInterface
    public void invoke(String str) {
        Log.i(a, "JS invoke");
        JSONObject a = c.a(str);
        if (a == null) {
            Log.e(a, "JS invoke callJson null");
            c.a(this.b, 13);
            return;
        }
        String optString = a.optString("_appId");
        String optString2 = a.optString("_method");
        String optString3 = a.optString("_args");
        String optString4 = a.optString("_index");
        String a2 = com.huawei.hwCloudJs.d.a.a(this.b);
        if (TextUtils.isEmpty(a2)) {
            Log.e(a, "JS invoke webViewUrl null");
            c.a(this.b, optString4, b.k, this.c);
        } else if (TextUtils.isEmpty(optString) && !b(a2)) {
            Log.e(a, "JS invoke JS_RET_CODE_METHOD_NOT_AUTH");
            c.a(this.b, optString4, 10, this.c);
        } else if (TextUtils.isEmpty(optString2)) {
            Log.e(a, "JS invoke method isEmpty");
            c.a(this.b, optString4, 13, this.c);
        } else if (optString2.equals("listenEvent")) {
            Log.i(a, "JS invoke method listenEvent");
            if (!a(optString3)) {
                Log.e(a, "JS invoke method listenEvent error");
                c.a(this.b, optString4, 13, this.c);
            }
        } else {
            a b = com.huawei.hwCloudJs.core.a.b.a().b(optString2);
            if (b == null) {
                Log.e(a, "JS invoke method cannot get!");
                c.a(this.b, optString4, 12, this.c);
                return;
            }
            a(optString, optString3, optString4, a2, b);
        }
    }

    public boolean onReceive(NativeMsg nativeMsg) {
        if (!(nativeMsg == null || TextUtils.isEmpty(nativeMsg.getType()))) {
            String a = com.huawei.hwCloudJs.d.a.a(this.b);
            if (!TextUtils.isEmpty(a)) {
                String type = nativeMsg.getType();
                if ("channelMessage".equals(type) && (nativeMsg instanceof ChannelMessageReq)) {
                    ChannelMessageReq channelMessageReq = (ChannelMessageReq) nativeMsg;
                    if (this.e.containsKey(com.huawei.hwCloudJs.c.a.c + channelMessageReq.getChannelName())) {
                        a(channelMessageReq, a);
                    }
                } else if (this.e.containsKey(type)) {
                    a(nativeMsg, a);
                }
            }
        }
        return true;
    }

    public void setBridgeId(String str) {
        this.c = str;
    }
}
